package filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpSession session=((HttpServletRequest)servletRequest).getSession();
        servletResponse.setCharacterEncoding("utf-8");
        if(session.getAttribute("user")==null){			//判断session是否由user这个对象
            PrintWriter out=servletResponse.getWriter();		//创建一个输出流
            //如果为空则通过javascript脚本输出提示并跳转到index页面
            out.println("<script language=javascript>alert('您还没有登录');window.location.href='/studentlogin.html';</script>");
        }else{
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }
}
